<template>
	<view>
		<web-view :src="urls" ref="webviewRef"></web-view>
	</view>
</template>
<script>
	import {
		Api
	} from "@/utils/Api.js";
	import {
		mapState
	} from "vuex";

	export default {
		data() {
			return {
				describe: "通用查看",
				url: {
					show: "/m/visual/moduleShowPage",
				},
				urls: "",
				moduleName: "",
			};
		},
		onLoad(options) {
			console.log('options', options);
			let rec = decodeURIComponent(options.record);
			let records = JSON.parse(rec);
			console.log('this.loginInfo', this.loginInfo);

			console.log('records', records);			//动态获取webview链接
			if (records.id) {
				this.moduleName = records.moduleName;
				uni.setNavigationBarTitle({ title: this.moduleName });
				let moduleCode = '';
				if (records.moduleCode) {
					moduleCode = records.moduleCode;
				} else {
					moduleCode = records.code;
				}
				//编辑
				this.urls =
					Api.baseUrl +
					this.url.show +
					"?skey=" +
					this.loginInfo.skey +
					"&moduleCode=" +
					moduleCode +
					"&id=" +
					records.id +
					"&isUniWebview=true";
			} else {
				uni.showToast({
					title: '缺少ID',
				});
			}
		},
		computed: {
			...mapState(["loginInfo"]),

			// urls() {
			//   let urls = Api.baseUrl + this.url.list + this.loginInfo.skey;
			//   return urls;
			// },
		},
		onShow() {
			if (this.isFromNavigateBack) {
			  console.log('由 navigateBack 触发返回');
			  this.isFromNavigateBack = false;
			}
		},
		methods: {
			callWebViewRefreshMethod() {
				// 调用 module_show.html中的 refreshListForRelate()
				let params = { action: 'refreshListForRelate' };

				// #ifdef H5
				const iframe = document.querySelector('iframe');
				if (iframe) {
					const channel = new MessageChannel();
					// 跨域发送端口给子页面
					iframe.contentWindow.postMessage(
					  { type: 'INIT_PORT' }, 
					  Api.baseUrl, 
					  [channel.port2] // 传输端口
					);
					
					// 通过port1端口监听子页面消息
					channel.port1.onmessage = (event) => {
					  console.log('收到子页面消息:', event.data);
					};
					// 通过 port1 发送消息
					channel.port1?.postMessage(params);
				}
				// #endif
				// #ifdef APP-PLUS
				const webview = this.$refs.webviewRef;
				console.log('webview.evalJS', webview?.evalJS);
				
				/* const pages = getCurrentPages();
				const currentPage = pages[pages.length - 1];
				const myWebview = currentPage.$getAppWebview();
				console.log('myWebview', myWebView);
				if (myWebview?.evalJS) {
					console.log('myWebview.evalJS', myWebview.evalJS);
					// 跨域无效						
					// setTimeout(() => {
					// 	myWebview.evalJS(`window.refreshListForRelate(${JSON.stringify(params)})`);
					// }, 500);
					
					// 跨域也无效
					// myWebview.evalJS(`window.postMessage(${JSON.stringify(params)}, '*')`);
				} */
				
				// 通过 Hash 传参，可以跨域
				let time = new Date().getTime();
				const newUrl = `${this.urls}#action=refreshListForRelate&t=` + time;
				webview.setAttribute('src', newUrl); // 重新加载 URL（或使用 iframe）
				// #endif
				
				// #ifdef MP-WEIXIN
				this.urls = `${this.urls}#action=refreshListForRelate&t=` + new Date().getTime();
				// #endif
			}
		}
	};
</script>
<style lang="scss" scoped>
</style>
